package com.xiaoshuidi.cloud.module.finance.controller.admin.finance;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.tenant.core.context.TenantContextHolder;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.FinanceAccountBalanceWarningReqVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceAccountBalanceWarningDO;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountBalanceWarningService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Tag(name = "管理后台 - 账户余额预警")
@RestController
@RequestMapping("/finance/account")
public class FinanceAccountBalanceWarningController {
    @Resource
    private FinanceAccountBalanceWarningService financeAccountBalanceWarningService;

    @PostMapping("/balance-warning/update")
    @Operation(summary = "设置预警")
    @PreAuthorize("@ss.hasPermission('finance:account:balance:warning')")
    public CommonResult<Integer> create(@RequestBody FinanceAccountBalanceWarningReqVO reqVO) {
        FinanceAccountBalanceWarningDO financeAccountBalanceWarningDO =
                financeAccountBalanceWarningService.getByCustType(reqVO.getCustType());
        if (financeAccountBalanceWarningDO != null) {
            financeAccountBalanceWarningService.update(financeAccountBalanceWarningDO.getId(), reqVO, TenantContextHolder.getTenantId());
        } else {
            financeAccountBalanceWarningService.save(reqVO, TenantContextHolder.getTenantId());
        }
        return CommonResult.success();
    }

    @PostMapping("/balance-warning/get")
    @Operation(summary = "获取预警设置")
    @PreAuthorize("@ss.hasPermission('finance:account:balance:warning')")
    public CommonResult<FinanceAccountBalanceWarningDO> create(@RequestParam("custType") String custType) {
        return CommonResult.success(financeAccountBalanceWarningService.getByCustType(custType));
    }

}
